package com.linln.modules.order.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linln.common.enums.OrderEnum;
import com.linln.common.enums.RoleEnum;
import com.linln.common.exception.order.OrderException;
import com.linln.common.vo.JsonResult;
import com.linln.modules.order.domain.OrderTakecaruser;
import com.linln.modules.order.domain.YearauditOrder;
import com.linln.modules.order.repository.OrderTakecaruserMapper;
import com.linln.modules.order.repository.YearauditOrderMapper;
import com.linln.modules.order.service.OrderTakecaruserService;
import com.linln.modules.order.service.YearauditOrderService;
import com.linln.modules.shopsService.repository.ShopsRepository;
import com.linln.modules.system.domain.User;
import com.linln.modules.system.repository.UserRepository;
import com.linln.modules.system.service.RoleService;
import com.linln.modules.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

/**
 * @author 薛超
 * @since 2021-01-11
 * @version 1.0.7
 */
@Service
public class OrderTakecaruserServiceImpl extends ServiceImpl<OrderTakecaruserMapper, OrderTakecaruser> implements OrderTakecaruserService {

    @Autowired
    private UserService userService;


    @Autowired
    private ShopsRepository shopsRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private YearauditOrderMapper yearauditOrderMapper;

    @Autowired
    private YearauditOrderService yearauditOrderService;


    @Override
    @Transactional
    public void saveTakecaruser(User user) throws OrderException {
        User newUser = userService.save(user);
        //店铺那边接口刚好有写好的方法。。直接掉来用
        int count = shopsRepository.createUserRole(user.getId(), RoleEnum.TAKECARUSER.getCode());
        if(newUser == null|| count<1){
            throw new OrderException("取车人用户添加失败");
        }
    }

    @Override
    @Transactional
    public int updateTakecaruser(User user) {
        int flag = userRepository.updateTakecaruser(user.getId(),user.getNickname(),user.getPhone(),user.getRemark());
        return flag;
    }

    @Override
    @Transactional
    public void removeTakecaruserById(String id) {
        userRepository.deleteById(Long.valueOf(id));
        //shopsRepository.delShopRole(Long.valueOf(id),RoleEnum.TAKECARUSER.getCode());
    }

    @Override
    public boolean isHaveOrder(String id) {
        List<YearauditOrder> yearauditOrderList =  yearauditOrderMapper.findPickUTheCar("","", OrderEnum.WAITINGCAR.getCode(),id,"");
        if(yearauditOrderList.size()>0){
            return false;
        }
        return true;
    }
}
